//
/* 
Module dependencies
*/
var userModel = require('../models/userModel.js');


var Base = require('./baseCtrl.js');
var util = require('util');

function userCtrl() {
	Base.apply(this, arguments);
}

util.inherits(userCtrl,Base);
var p = Base.prototype;

p.get = {
	"/register": function(req,res) {
		res.render('register_form.html');
		console.log('GET reqt to /register.');
	},

	"/login": function(req,res) {
		res.render('login_form.html');
		console.log('GET reqt to /login');
	}
}

p.post = {
	"/registrate": function(req,res) {
		res.redirect('/');
		console.log(req.body.usr);
		console.log(req.body.pwd);
		usr = req.body.usr;
		pwd = req.body.pwd;
		userModel.setRR(req,res);
		userModel.create(usr,pwd,function(user_id) {
			//gameModel.initUser(user_id,function() {
			//	//refresh after initialzation; 
			//});
		});
	},

	"/auth": function(req,res) {
		var password = req.body.password;
		var username = req.body.username;
		console.log(username);
		//userModel.setRR(req,res); //i have a feeling this might be bad..
		userModel.auth(req,res,username,password, function(err,auth) {
			if(auth) {
				console.log('login succesful set session etc.');
				console.log(req.session.uid);
			} else {
				console.log(err); //do not specify what is wrong to the user, for security!
				//send "username and/or password does not match" to the user;
			}
		});
	}

}

module.exports = new userCtrl('User Controller');


